package spcpy

import (
	"gitee.com/ymofen/golog"
	"gitee.com/ymofen/sputils"
)

type SPCpy struct {
	Id  string
	log golog.Logger
	sub *sputils.SpBaseSub
	pub *sputils.SpBasePub
}

func (this *SPCpy) Init() {
	this.sub = sputils.NewSpBaseSub("")
	this.pub = sputils.NewSpBasePub("")
	this.log = golog.Default().WithOptionEx(golog.WithOptionPrefix(this.Id))
}

func (this *SPCpy) Close() error {
	this.sub.Close()
	this.pub.Close()
	return nil
}

func (this *SPCpy) onInput(id, topic string, args ...interface{}) (ok bool) {
	action := args[0].(int)
	if action == sputils.SpTypeData {
		buf := args[1].([]byte)
		this.pub.Pub("", 0, buf)
	}
	return
}

func (this *SPCpy) UpdateConf(subconf string, pubconf string) {
	this.sub.ReloadConfString(subconf, this.onInput)
	this.pub.ReloadConfString(pubconf, func(id, topic string, args ...interface{}) (ok bool) {
		return true
	})
}
